package com.ldzy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class PageController {
    @GetMapping("logout")
    public String logout(HttpSession session){
        //退出登录，服务器删除session
        session.invalidate();
        return "login";
    }
    @GetMapping("toOrderInfoPage")
    public ModelAndView toOrderInfoPage(){//返回登录页面
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("orderinfo");
        return modelAndView;
    }
    @GetMapping("toLoginPage")
    public ModelAndView toLoginPage(){//返回登录页面
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }
    @GetMapping("toMainPage")
    public ModelAndView toMainPage(){//返回首页
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        return modelAndView;
    }
    @PostMapping("login")
    public ModelAndView login(String username,String pwd,HttpSession session){
        //获取账号密码，检查是否正确，正确则跳转的首页，否则要求重输
        ModelAndView modelAndView = new ModelAndView();
        if("tom".equals(username)&&"123".equals(pwd)){
            //登录成功，在新的session中添加键值对，跳转到首页
            session.setAttribute("user",username);
            modelAndView.addObject("msg","欢迎光临："+username);
            modelAndView.setViewName("main");
        }else {//登录失败，跳转到登录页面
            modelAndView.addObject("msg","您输入的账号密码不正确，请重新输入！！");
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

}
